<?php
namespace YmPhp\Admin\Http\Controllers\Member\Level;
use YmPhp\Admin\Http\Controllers\Channel\Wechat\WechatMenuController;
use YmPhp\Admin\Models\Member\MemberList;
use YmPhp\Admin\Models\Member\MemberLevelList;
use YmPhp\Admin\Form;
use YmPhp\Admin\Grid;
use YmPhp\Admin\Show;
use YmPhp\Admin\Http\Controllers\AdminController;

class MemberLevelListController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new MemberLevelList(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('uid')->display(function (){
                $memberInfo = (new MemberList())->getDetails($this->uid);
                $member = '用户名称：'.$memberInfo->username.'<br>';
                $member .= '手机号：'.$memberInfo->mobile.'<br>';
                return $member;
            });
            $grid->column('price');
            $grid->column('cardnum')->display(function (){
              return '激活码：'.$this->cardnum;
            });
            $grid->column('cardinfo')->display(function (){
                $data = unserialize($this->cardinfo);
                $cardData = '<div>名称：'.$data[0].'</div>';
                $cardData .= '<div>时长：'.$data[1].'天</div>';
                $cardData .= '<div>价格：'.$data[2].'元</div>';
                $cardData .= '<div>原价：'.$data[3].'元</div>';
                $cardData .= '<div>描述：'.$data[4].'</div>';
                return $cardData;
            });
            $grid->column('tiyannum');
            $grid->column('payts')->display(function (){
                return $this->payts ? date('Y-m-d H:i:s', $this->payts) : '';
            })->width(100);
            $grid->column('status')->select([
                0 => '未支付',
                1 => '已支付',
                2 =>'已失效'
            ]);
            $grid->column('endts')->display(function (){
                return $this->endts ? date('Y-m-d H:i:s', $this->endts) : '';
            })->width(100);
            $grid->column('created_at')->width(100)->sortable();
            $grid->column('updated_at')->width(100)->sortable();
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new MemberLevelList(), function (Show $show) {
            $show->field('id');
            $show->field('uid');
            $show->field('order_id');
            $show->field('price');
            $show->field('note');
            $show->field('payts');
            $show->field('status');
            $show->field('cardnum');
            $show->field('cardinfo');
            $show->field('usetime');
            $show->field('stid');
            $show->field('tiyannum');
            $show->field('endts');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new MemberLevelList(), function (Form $form) {
            $form->display('id');
            $form->text('uid');
            $form->text('order_id');
            $form->text('price');
            $form->text('payts');
            $form->text('status')->options([0 => '未支付', 1 => '已支付', 2 =>'已失效']);
            $form->display('cardnum');
            $form->display('cardinfo');
            $form->text('note');
            $form->text('stid');
            $form->text('tiyannum');
            $form->datetime('endts')->format('YYYY-MM-DD HH:mm:ss');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
